module EmailAndUserAuthenticating

  attr_accessor :login
  def self.included(base)

    base.validates_presence_of :username, :email

    def base.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(['lower(username) = :value OR lower(email) = :value', {value: login.downcase}]).first
      else
        where(conditions).first
      end
    end
  end
end